package com.mall.market.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.market.mapper.SmsHomeBrandMapper;
import com.mall.model.domain.SmsHomeBrand;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.PageResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SmsHomeBrandService {

    @Autowired
    private SmsHomeBrandMapper smsHomeBrandMapper;


    public CommonPage HomeBrandList(String brandName, Integer pageNum, Integer pageSize, Integer recommendStatus) {
        Page page = new Page(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeBrand> wrapper = new LambdaQueryWrapper<SmsHomeBrand>();
        wrapper.like(StringUtils.isNotEmpty(brandName), SmsHomeBrand::getBrandName, brandName);
        wrapper.eq(recommendStatus!=null,SmsHomeBrand::getRecommendStatus, recommendStatus);
        wrapper.orderByDesc(SmsHomeBrand::getSort);
        Integer count = smsHomeBrandMapper.selectCount(wrapper);
        Page pages = smsHomeBrandMapper.selectPage(page, wrapper);
        return new CommonPage(pageNum, pageSize, count, pages.getRecords());
    }

    public void addBrand(SmsHomeBrand smsHomeBrand) {
        smsHomeBrandMapper.insert(smsHomeBrand);
    }

    public void removeBrand(List<Integer> ids) {
        LambdaQueryWrapper<SmsHomeBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SmsHomeBrand::getId, ids);
        smsHomeBrandMapper.delete(wrapper);
    }

    public void updateBrandStatus(List<Integer> ids, Integer recommendStatus) {
        LambdaQueryWrapper<SmsHomeBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SmsHomeBrand::getId, ids);
        List<SmsHomeBrand> smsHomeBrands = smsHomeBrandMapper.selectList(wrapper);
        for (SmsHomeBrand smsHomeBrand : smsHomeBrands) {
            if (smsHomeBrand != null) {
                smsHomeBrand.setRecommendStatus(recommendStatus);
                smsHomeBrandMapper.updateById(smsHomeBrand);
            }
        }
    }

    public void updateSort(Integer id, Integer sort) {
        SmsHomeBrand smsHomeBrand = smsHomeBrandMapper.selectById(id);
        if (smsHomeBrand != null) {
            smsHomeBrand.setSort(sort);
            smsHomeBrandMapper.updateById(smsHomeBrand);
        }
    }
}
